package com.example.backend.controller;


import com.example.backend.pojo.Manage;
import com.example.backend.pojo.Restaurant;
import com.example.backend.pojo.Result;
import com.example.backend.pojo.User;
import com.example.backend.service.*;
import com.example.backend.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private UserService userService;
    @Autowired
    private RestaurantService restaurantService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private ManageService manageService;
    @Autowired
    private ComplainService complainService;

    @PostMapping("/register")//注册
    public Result register(String name, String password){
        Manage manage=manageService.getByName(name);
        if(manage==null){
            manageService.register(name, password);
            return Result.success();
        }else{
            return Result.error("管理员注册失败");
        }
    }

    @PostMapping("/login")//登录
    public Result<String> login(String managename,String password){
        Manage manage =manageService.getByName(managename);
        //查看用户状态是否注册，再检查用户名是否纯在，并且检查是否密码正确
        if(manage==null){
            return Result.error("用户名错误");
        }
        if(password.equals(manage.getPassword())){
            Map<String ,Object> claims=new HashMap<>();
            claims.put("name",manage.getManagename());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    //获得所有用户
    @GetMapping("/usergetall")
    public Result<List<User>> getalluser(){
        return  Result.success(userService.getall());
    }

    //获得所有餐厅
    @GetMapping("/resgetall")
    public Result<List<Restaurant>> getallrestaurant(){
        List<Restaurant> restaurants = restaurantService.getall();
        for(Restaurant restaurant:restaurants){
            restaurant.setAddressareaid(addressService.getallname(Integer.valueOf(restaurant.getAddressareaid())));
        }
        return  Result.success(restaurants);
    }

    @GetMapping("/getallnormalres")
    public Result<List<Restaurant>> getallnormalres(){
        return  Result.success(restaurantService.getallnormalres());
    }

    @PostMapping("/rejectres")
    public Result rejectres(String inform,String restaurantid)
    {
        restaurantService.rejectres(inform,restaurantid);
        return  Result.success();
    }

    @PostMapping("/addinform")
    public Result addinform(String inform,String restaurantid)
    {
        restaurantService.addinform(inform,restaurantid);
        return  Result.success();
    }

}
